package com.nus.fellowmark.modules.assignment.service.impl;

import com.nus.fellowmark.entity.AssignmentEntity;
import com.nus.fellowmark.modules.assignment.dto.ConfigDTO;
import com.nus.fellowmark.modules.assignment.service.AssignmentService;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nus.fellowmark.common.utils.PageUtils;
import com.nus.fellowmark.common.utils.Query;

import com.nus.fellowmark.dao.GradeCalculationConfigDao;
import com.nus.fellowmark.entity.GradeCalculationConfigEntity;
import com.nus.fellowmark.modules.assignment.service.GradeCalculationConfigService;

import javax.annotation.Resource;


@Service("gradeCalculationConfigService")
public class GradeCalculationConfigServiceImpl extends ServiceImpl<GradeCalculationConfigDao, GradeCalculationConfigEntity> implements GradeCalculationConfigService {

    @Resource
    GradeCalculationConfigDao gradeCalculationConfigDao;

    @Resource
    AssignmentService assignmentService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<GradeCalculationConfigEntity> page = this.page(
                new Query<GradeCalculationConfigEntity>().getPage(params),
                new QueryWrapper<GradeCalculationConfigEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public boolean addOrUpdateConfig(ConfigDTO configDTO) {
        if (configDTO == null) {
            return false;
        }
        GradeCalculationConfigEntity configEntity = gradeCalculationConfigDao.selectOne(new QueryWrapper<GradeCalculationConfigEntity>().eq("assignment_id", configDTO.getAssignmentId()));
        GradeCalculationConfigEntity gradeCalculationConfigEntity = new GradeCalculationConfigEntity();
        gradeCalculationConfigEntity.setAssignmentId(configDTO.getAssignmentId());
        gradeCalculationConfigEntity.setPtcWeight(configDTO.getPtcWeight() == null? 0 : configDTO.getPtcWeight());
        gradeCalculationConfigEntity.setIncludeHighest(configDTO.getIncludeHighest() == null? 1 : configDTO.getIncludeHighest());
        gradeCalculationConfigEntity.setIncludeLowest(configDTO.getIncludeLowest() == null? 1 : configDTO.getIncludeLowest());
        gradeCalculationConfigEntity.setIncludeOutliers(configDTO.getIncludeOutliers() == null? 1 : configDTO.getIncludeOutliers());
        gradeCalculationConfigEntity.setIncludeParticipation(configDTO.getIncludeParticipation() == null? 0: configDTO.getIncludeParticipation());
        if (configEntity == null) {
            AssignmentEntity assignmentEntity = assignmentService.getById(configDTO.getAssignmentId());
            if (assignmentEntity == null) {
                return false;
            }
            return gradeCalculationConfigDao.insert(gradeCalculationConfigEntity) > 0;
        } else {
            return gradeCalculationConfigDao.updateById(gradeCalculationConfigEntity) > 0;
        }
    }

    @Override
    public GradeCalculationConfigEntity getGradeConfig(Integer assignmentId) {
        return gradeCalculationConfigDao.selectOne(new QueryWrapper<GradeCalculationConfigEntity>().eq("assignment_id", assignmentId));
    }

    @Override
    public boolean initConfig(Integer id) {
        ConfigDTO configDTO = new ConfigDTO(id, 0, 0.0, 1, 1, 1);
        return addOrUpdateConfig(configDTO);
    }

}